"""
@File    :   router.py
@Time    :   2024/03/27 15:54:52
@Author  :   Qrj
@Email   :   923207736@qq.com
@description   :   Token相关路由
"""

from typing import Annotated

from fastapi import APIRouter, Depends

from src.business.admin.system.oauth2.token.dependencies.no_introduction import (
    DependVerifyAccessTokenMenumPermission,
)
from src.business.admin.system.oauth2.token.schemas.out import (
    DoubleTokenCompleteOutSchema,
)
from src.enums.permission_operation import PermissionOperationEnum
from src.utils.unified_response import SuccessApiJSONResponse

from .dependencies.route_depend import (
    route_depend_force_logout_double_token,
    route_depend_list_access_token,
    route_depend_logout_self_double_token,
    route_depend_refresh_access_token,
)
from .schemas.out import AccessTokenCompleteOutSchema

# 查
depend_select_permission_verify = DependVerifyAccessTokenMenumPermission(
    __file__, PermissionOperationEnum.select.value
)

# 删
depend_force_logout_double_token_permission_verify = (
    DependVerifyAccessTokenMenumPermission(
        __file__, PermissionOperationEnum.force_logout_double_token.value
    )
)

# 创建路由
router = APIRouter(
    prefix="/admin/system/oauth2/token",
    tags=[
        "后台Token接口",
    ],
)


@router.post("/refresh-access-token", summary="使用刷新令牌刷新访问令牌")
async def route_refresh_access_token(
    double_token: Annotated[
        DoubleTokenCompleteOutSchema, Depends(route_depend_refresh_access_token)
    ],
):
    access_token, refresh_token = double_token.access_token, double_token.refresh_token
    # 构建返回信息
    data = {
        "access_token": access_token.access_token,
        "access_token_expiration_datetime": access_token.expiration_datetime,
        "refresh_token": refresh_token.refresh_token,
        "refresh_token_expiration_datetime": refresh_token.expiration_datetime,
        "token_type": access_token.token_type,
    }
    return SuccessApiJSONResponse(message="访问令牌刷新成功", data=data)


@router.post("/list")
async def route_list_access_token(
    verify_menu_permission: Annotated[None, Depends(depend_select_permission_verify)],
    list_result: Annotated[
        tuple[list[AccessTokenCompleteOutSchema], int],
        Depends(route_depend_list_access_token),
    ],
):
    search_pagination_result, search_total = list_result
    data = {"result": search_pagination_result, "total": search_total}
    return SuccessApiJSONResponse(data=data)


@router.post("/logout", summary="注销登录")
async def route_logout_double_token(
    return_none: Annotated[None, Depends(route_depend_logout_self_double_token)],
):
    # 构建响应信息
    return SuccessApiJSONResponse(message="注销登录成功")


@router.post("/force-logout")
async def route_force_logout_double_token(
    verify_menu_permission: Annotated[
        None, Depends(depend_force_logout_double_token_permission_verify)
    ],
    force_logout_operation: Annotated[
        None, Depends(route_depend_force_logout_double_token)
    ],
):
    return SuccessApiJSONResponse()
